﻿using System;

namespace Adaptive.Foundation.Tokenizers
{
    /// <summary>
    /// Provides static/shared methods and functions related to character token and 
    /// tokenizing operations.
    /// </summary>
    /// <remarks>
    /// 
    /// </remarks>
    public static class CharacterTokenFactory
    {
        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Gets the enumerated value for the specified character.
        /// </summary>
        /// <param name="character">
        /// A string containing the character to be mapped.</param>
        /// <remarks>
        /// Only the first character of the provided string is examined.  If the
        /// string is null or empty, a null character (0) is assumed.
        /// </remarks>
        /// <returns>
        /// A <see cref="CharacterTokenValues"/> enumerated value representing
        /// the character.
        /// </returns>
        public static CharacterTokenValues GetTokenValue(string character)
        {
            if (string.IsNullOrEmpty(character))
                return GetTokenValue((char)0);
            else
                return GetTokenValue(character[0]);
        }
        /// <summary>
        /// Gets the enumerated value for the specified character.
        /// </summary>
        /// <param name="character">
        /// Tthe character to be mapped.
        /// </param>
        /// <returns>
        /// A <see cref="CharacterTokenValues"/> enumerated value representing
        /// the character.
        /// </returns>
        public static CharacterTokenValues GetTokenValue(char character)
        {
            CharacterTokenValues returnValue = CharacterTokenValues.None;

            switch (character)
            {
                case (char)0:
                    returnValue = CharacterTokenValues.NullCharacter;
                    break;
                case '-':
                    returnValue = CharacterTokenValues.Dash ;
                    break;
                case ',':
                    returnValue = CharacterTokenValues.Comma;
                    break;
                case ';':
                    returnValue = CharacterTokenValues.SemiColon;
                    break;
                case ':':
                    returnValue = CharacterTokenValues.Colon;
                    break;
                case '/':
                    returnValue = CharacterTokenValues.Slash;
                    break;
                case '\\':
                    returnValue = CharacterTokenValues.Backslash;
                    break;
                case '.':
                    returnValue = CharacterTokenValues.Period;
                    break;
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    returnValue = CharacterTokenValues.NumericDigit;
                    break;
                case '(':
                    returnValue = CharacterTokenValues.OpenParenthesis;
                    break;
                case '[':
                    returnValue = CharacterTokenValues.OpenBracket ;
                    break;
                case '{':
                    returnValue = CharacterTokenValues.OpenBrace ;
                    break;
                case '<':
                    returnValue = CharacterTokenValues.OpenAngle ;
                    break;
                case '>':
                    returnValue = CharacterTokenValues.CloseAngle ;
                    break;
                case '}':
                    returnValue = CharacterTokenValues.CloseBrace ;
                    break;
                case ']':
                    returnValue = CharacterTokenValues.CloseBracket ;
                    break;
                case ')':
                    returnValue = CharacterTokenValues.CloseParenthesis;
                    break;
                case '\'':
                    returnValue = CharacterTokenValues.SingleQuote;
                    break;
                case '"':
                    returnValue = CharacterTokenValues.DoubleQuote;
                    break;
                case '?':
                    returnValue = CharacterTokenValues.QuestionMark;
                    break;
                case '~':
                    returnValue = CharacterTokenValues.Tilde;
                    break;
                case '!':
                    returnValue = CharacterTokenValues.ExclamationMark;
                    break;
                case '@':
                    returnValue = CharacterTokenValues.AtSign;
                    break;
                case '#':
                    returnValue = CharacterTokenValues.NumberSign;
                    break;
                case '$':
                    returnValue = CharacterTokenValues.DollarSign;
                    break;
                case '%':
                    returnValue = CharacterTokenValues.PercentSign;
                    break;
                case '^':
                    returnValue = CharacterTokenValues.Caret;
                    break;
                case '&':
                    returnValue = CharacterTokenValues.Ampersand;
                    break;
                case '*':
                    returnValue = CharacterTokenValues.Asterisk;
                    break;
                case ' ':
                    returnValue = CharacterTokenValues.Space;
                    break;
                case '+':
                    returnValue = CharacterTokenValues.AdditionSign;
                    break;
                case '=':
                    returnValue = CharacterTokenValues.EqualsSign;
                    break;
                case '\r':
                    returnValue = CharacterTokenValues.CarriageReturn;
                    break;
                case '\t':
                    returnValue = CharacterTokenValues.Tab;
                    break;
                case '\n':
                    returnValue = CharacterTokenValues.Linefeed;
                    break;
                case '\b':
                    returnValue = CharacterTokenValues.Backspace;
                    break;
                case '\u0007':
                    returnValue = CharacterTokenValues.Bell;
                    break;
                default:
                    returnValue = CharacterTokenValues.Undefined;
                    break;
            }
            return returnValue;
        }
        /// <summary>
        /// Gets the character for specified token value.
        /// </summary>
        /// <param name="charToken">
        /// A <see cref="CharacterTokenValues"/> enumerated value indicating the token value.
        /// </param>
        /// <returns>
        /// A character to represent the enumerated value.
        /// </returns>
        public static char GetCharacterForToken(CharacterTokenValues charToken)
        {
            char returnValue = '\0';

            switch (charToken)
            {
                case CharacterTokenValues.AdditionSign:
                    returnValue = '+';
                    break;
                case CharacterTokenValues.Ampersand:
                    returnValue = '&';
                    break;
                case CharacterTokenValues.Asterisk:
                    returnValue = '*';
                    break;
                case CharacterTokenValues.AtSign:
                    returnValue = '@';
                    break;
                case CharacterTokenValues.Bell:
                    returnValue = '\u0007';
                    break;
                case CharacterTokenValues.Backslash:
                    returnValue = '\\';
                    break;
                case CharacterTokenValues.Backspace:
                    returnValue = '\b';
                    break;
                case CharacterTokenValues.Caret:
                    returnValue = '^';
                    break;
                case CharacterTokenValues.CarriageReturn:
                    returnValue = '\r';
                    break;
                case CharacterTokenValues.CloseAngle:
                    returnValue = '>';
                    break;
                case CharacterTokenValues.CloseBrace:
                    returnValue = '}';
                    break;
                case CharacterTokenValues.CloseBracket:
                    returnValue = ']';
                    break;
                case CharacterTokenValues.CloseParenthesis:
                    returnValue = ')';
                    break;
                case CharacterTokenValues.Colon:
                    returnValue = ':';
                    break;
                case CharacterTokenValues.Comma:
                    returnValue = ',';
                    break;
                case CharacterTokenValues.Dash:
                    returnValue = '-';
                    break;
                case CharacterTokenValues.DollarSign:
                    returnValue = '$';
                    break;
                case CharacterTokenValues.DoubleQuote:
                    returnValue = '"';
                    break;
                case CharacterTokenValues.EqualsSign:
                    returnValue = '=';
                    break;
                case CharacterTokenValues.ExclamationMark:
                    returnValue = '!';
                    break;
                case CharacterTokenValues.HighOrderCharacter:
                    returnValue = '\u0128';
                    break;
                case CharacterTokenValues.HighOrderMaxAscii:
                    returnValue = '\u0255';
                    break;
                case CharacterTokenValues.Linefeed:
                    returnValue = '\n';
                    break;
                case CharacterTokenValues.NumberSign:
                    returnValue = '#';
                    break;
                case CharacterTokenValues.NumericDigit:
                    returnValue = '0';
                    break;
                case CharacterTokenValues.OpenAngle:
                    returnValue = '<';
                    break;
                case CharacterTokenValues.OpenBrace:
                    returnValue = '{';
                    break;
                case CharacterTokenValues.OpenBracket:
                    returnValue = '[';
                    break;
                case CharacterTokenValues.OpenParenthesis:
                    returnValue = '(';
                    break;
                case CharacterTokenValues.PercentSign:
                    returnValue = '%';
                    break;
                case CharacterTokenValues.Period:
                    returnValue = '.';
                    break;
                case CharacterTokenValues.QuestionMark:
                    returnValue = '?';
                    break;
                case CharacterTokenValues.SemiColon:
                    returnValue = ';';
                    break;
                case CharacterTokenValues.SingleQuote:
                    returnValue = '\'';
                    break;
                case CharacterTokenValues.Slash:
                    returnValue = '/';
                    break;
                case CharacterTokenValues.Space:
                    returnValue = ' ';
                    break;
                case CharacterTokenValues.Tab:
                    returnValue = '\t';
                    break;
                case CharacterTokenValues.Tilde:
                    returnValue = '~';
                    break;
            }
            return returnValue;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion
    }
}